(function() {

/**
 * @author Igor Lazarev
 */

WireIt.WebUIOutputContainer = function(options, layer) {

WireIt.WebUIOutputContainer.superclass.constructor.call(this, options, layer);

}

YAHOO.extend(WireIt.WebUIOutputContainer, WireIt.WebUIFormContainer, {
	
	setOptions: function(options){
		WireIt.WebUIOutputContainer.superclass.setOptions.call(this,options);
		
		this.options.value = "";
		this.options.oldValue = "";
	},
	
	renderViewElement: function(url){
		if ((this.viewElement)&&this.viewElement.parentNode){
			this.bodyEl.removeChild(this.viewElement);
		}
		
		var viewLink ='<font color="#3366CC"><u>View</u></font>';
		this.viewElement = inputEx.cn('div',null,{},viewLink);
		this.viewElement.style.cursor = "pointer";
		
		this.bodyEl.appendChild(this.viewElement);
		if(url.endsWith('.m')){
			this.viewElement.onclick = function(){
				window.open('/viewm/view_m.html?'+url,'View'); 			
			}.bind(this);
		}
		else if(url.endsWith('.png')||url.endsWith('.jpg')||url.endsWith('.gif')||url.endsWith('.bmp')||url.endsWith('.svg')||url.endsWith('.tiff')){
			this.viewElement.onclick = function(){
				window.open(url,'View');		
			}.bind(this);
		}
		else{
			this.viewElement.onclick = function(){
				window.open(url,'View');
			}.bind(this);
		}
	},
	
	getConfig: function() {
	 var obj = WireIt.WebUIOutputContainer.superclass.getConfig.call(this);
		
		if(typeof(this.schema)!="undefined"){
			obj.schema = this.schema;
		}
		else{
			obj.schema = {};
		}
		if(typeof(obj.schema.title)=="undefined"){
			obj.schema.title = this.getValue().input.inputParams.name;
		}
		if(typeof(obj.schema.type)=="undefined"){
			obj.schema.type = this.getValue().input.type;
		}
		obj.term = this.terminals[0].options.name;

     return obj;	
	},
	
	onMouseDown: function() { //if the container is selected
	
	  if(this.layer) {
	  			
		 if(this.layer.focusedContainer && this.layer.focusedContainer != this) {
	        this.layer.focusedContainer.removeFocus();
			this.options.oldValue = this.getValue();
		}
		 else if (!this.layer.focusedContainer) {
			this.options.oldValue = this.getValue();
		 }
		
		 this.setFocus();
	     this.layer.focusedContainer = this;
		 this.layer.focusContainer(this); //fire a 'container focused' event
	  }
	
	},
	
	onResize: function(event, args){
		WireIt.WebUIOutputContainer.superclass.onResize.call(this,event, args);
		this.positionTerminals();
	},

	positionTerminals: function(){
		var width = WireIt.getIntStyle(this.el, "width");
		var term = this.terminals[0];
		 WireIt.sn(term.el, null, {position: "absolute", top: "-15px", left: (Math.floor(width/2)-15)+"px"});
	}, 
	
	removeFocus: function(){
		WireIt.WebUIOutputContainer.superclass.removeFocus.call(this);		
		
		this.options.value = this.getValue();
			
		 //check type changes
		 
		 if((this.options.value)&&(this.options.oldValue)){
				
				if (this.options.value.input.type != this.options.oldValue.input.type){
					this.initType();
					this.options.oldValue = this.options.value;
				}
				else if (this.options.value.input.inputParams.name != this.options.oldValue.input.inputParams.name){
					this.initType();
					this.options.oldValue = this.options.value;
				}
		 } 
	},
	
	initType: function(){
		
		if((this.terminals[0])&&this.getValue().input.type==this.terminals[0].options.ddConfig.type.split(":")[1]){
	
			var sameType = true;
			var wires = this.wires;
			var connected = new Array;
			
			for (i=0;i<wires.length;i++){
				connected[i] = wires[i].getOtherTerminal(this.terminals[0]);
			}
		} 
		
		
		this.removeAllTerminals();

		if (this.getValue().input.inputParams.name){
			this.addTerminal({xtype: "WireIt.util.WebUITerminalInput", 
								"name": this.getValue().input.inputParams.name + ":"+this.getValue().input.type,
								"ddConfig":{"type":YAHOO.lang.JSON.stringify(this.getValue().input.type)}, 
									"direction": [0,-1], 
									"offsetPosition": {"left": 86, "top": -15}});
			this.setName(this.getValue().input.inputParams.name);

		}
		else{
			var value = this.getValue();
			value.input.inputParams.name = "output";
			this.setValue(value);
			
			this.addTerminal({xtype: "WireIt.util.WebUITerminalInput", 
								"name": "output"+":"+this.getValue().input.type,
								"ddConfig":{"type":YAHOO.lang.JSON.stringify(this.getValue().input.type)}, 
									"direction": [0,-1], 
									"offsetPosition": {"left": 86, "top": -15}});
			
		}
		this.dd.setTerminals(this.terminals);
	
		if(sameType){ //the type of the container has not changed
			for (i=0;i<wires.length;i++){
				var wire = {"tgt":{"moduleId":WireIt.indexOf(this, this.layer.containers),"terminal":this.terminals[0].options.name},
							"src":{"moduleId":WireIt.indexOf(connected[i].container, this.layer.containers),"terminal": connected[i].options.name}};
				this.layer.addWire(wire);

			}
		}

		
	},
	
	initState: function(){
	
		this.setReady();
		if((this.terminals[0])&&(this.terminals[0].wires.length<1)){
			this.setUnready();
		}

	},
	
	setName: function(title){
		this.ddHandle.removeChild(this.title);
		this.title = WireIt.cn('span', null, null, title);
		this.ddHandle.appendChild(this.title);
	}	
	
})

})()
